//
//  HMSportMapViewController.m
//  LetsGo
//
//  Created by 段立峰 on 2016/11/9.
//  Copyright © 2016年 段立峰. All rights reserved.
//

#import "HMSportMapViewController.h"
#import <MAMapKit/MAMapKit.h>
@interface HMSportMapViewController ()<MAMapViewDelegate>

@end

@implementation HMSportMapViewController{
    /// 起始位置
    CLLocation *_startLocation;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setupMapView];
    
}

-(void)mapView:(MAMapView *)mapView didUpdateUserLocation:(MAUserLocation *)userLocation updatingLocation:(BOOL)updatingLocation{
    
    //0.判断“位置数据”是否变化，不一定是经纬度变化
    if(!updatingLocation){
        
        return;
    }
    
    NSLog(@"这里%@ %p",userLocation.location,userLocation.location);
    NSLog(@"%p",userLocation);
    
    if(_startLocation == nil){
        
        _startLocation = userLocation.location;
        //1.实例化大头针
        MAPointAnnotation * annotaion =[MAPointAnnotation new];
        //2.指定坐标位置
        annotaion.coordinate = userLocation.location.coordinate;
        //3.添加到地图视图
        [mapView addAnnotation:annotaion];
        
    }
    
    
}
-(MAAnnotationView *)mapView:(MAMapView *)mapView viewForAnnotation:(id<MAAnnotation>)annotation{
    //0,判断大头针的类型
    if(![annotation isKindOfClass:[MAPointAnnotation class]]){
        
        
        return nil;
    }
    //1.查询可重用大头针视图
    static NSString *annotaionId =@"annotaionId";
    MAAnnotationView *annotaionView = [mapView dequeueReusableAnnotationViewWithIdentifier:annotaionId];
    //2，如果没有查到，新建视图
    if(annotaionView == nil){
        annotaionView = [[MAAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:annotaionId];
        
        
    }
    //3.设置大头针视图-设置图像
    
    
    // 根据运动类型来创建运动图像
    UIImage *image = _sportTracking.sportImage;

    //根据运动类型来创建运动图像
    
//    UIImage *image = [UIImage imageNamed:@"map_annotation_bike"];
//    UIImage *image;
//    switch (_sportType) {
//        case HMSportTypeRun:
//            image = [UIImage imageNamed:@"map_annotation_run"];
//            break;
//        case HMSportTypeWalk:
//            image = [UIImage imageNamed:@"map_annotation_walk"];
//            break;
//        case HMSportTypeBike:
//            image = [UIImage imageNamed:@"map_annotation_bike"];
//            break;
//
//    }
    annotaionView.image = image;
    annotaionView.centerOffset = CGPointMake(0, -image.size.height *0.5);
    
    
    return annotaionView;
    
}
-(void)setupMapView{
    //1.实例化地图视图
    MAMapView *mapView = [[MAMapView alloc] initWithFrame:self.view.bounds];
    //2.添加到根视图
    [self.view addSubview:mapView];
    
    //3.隐藏比例尺
    mapView.showsScale = NO;
    
    //4.关闭相机旋转
    mapView.rotateCameraEnabled = NO;
    
    //5.显示用户位置
    mapView.showsUserLocation = YES;
    
    //6.跟踪用户位置 - 追踪用户的location与heading更新/会放大地图的显示，将用户设置在地图的中心点
    mapView.userTrackingMode = MAUserTrackingModeFollowWithHeading;
    //7.允许后台定位 - 可以将用户定位在地图的中心，并且放大地图，有的时候，速度会有些慢
    mapView.allowsBackgroundLocationUpdates = YES;
    //8.不允许系统暂停位置更新
    mapView.pausesLocationUpdatesAutomatically = NO;
    
    //9.设置代理
    mapView.delegate = self;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
